package com.cy.milktea.controller;

import com.cy.milktea.pojo.System.JsonResult;
import com.cy.milktea.pojo.User;
import com.cy.milktea.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping
    public JsonResult add(@RequestBody User user) {
        userService.add(user);
        return JsonResult.ok("添加成功!");
    }


    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable Integer id) {
        return userService.delete(id) == 0 ? JsonResult.error("当前id不存在用户") : JsonResult.ok("删除成功!");
    }


    @PutMapping
    public JsonResult update(@RequestBody User user) {
        userService.update(user);
        return JsonResult.ok("更新成功!");
    }

    @GetMapping
    public JsonResult queryAll(String name, Integer roleId, Integer valid) {
        if (name != null) name = "".equals(name.trim()) ? null : name.trim();
        roleId = roleId == -1 ? null : roleId;
        valid = valid == -1 ? null : valid;
        return JsonResult.ok("查询成功!", userService.query(name, roleId, valid));
    }

}










